import 'package:flutter/material.dart';
import 'package:flutter_study/config/ConstColor.dart';
import 'package:flutter_study/config/ConstString.dart';

class LoadingContainer extends StatelessWidget {
  final Widget child;
  final bool error;
  final bool loading;
  final VoidCallback retry;

  const LoadingContainer(
      {Key key,
      @required this.loading,
      @required this.child,
      this.error = false,
      @required this.retry})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (loading) {
      return _loadView;
    } else if (error) {
      return _errorView;
    }else {
      return child;
    }
  }

  Widget get _errorView {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset("images/ic_error.png", width: 100, height: 100),
          Padding(
            padding: const EdgeInsets.only(top: 8),
            child: Text(
              ConstString.net_error_tip,
              style: TextStyle(color: ConstColor.hitTextColor, fontSize: 18),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 8),
            child: OutlineButton(
              onPressed: () {
                retry.call();
              },
              child: Text(ConstString.reload_again),
              highlightColor: Colors.white,
              highlightedBorderColor: Colors.black12,
            ),
          )
        ],
      ),
    );
  }

  Widget get _loadView {
    return Center(
      child: CircularProgressIndicator(),
    );
  }
}
